DIM_KERNEL	equ	 35
[BITS 16]    
[ORG 0x7C00] 
xor ax, ax
mov ds, ax
mov ss,ax
mov sp,0xFFFE 
call load_kernel
call off_floppy
init_pmode:
	cli
	in	al,0x70	
	or	al,0x80
	out	0x70,al
.wait1:
  	in  al, 0x64
  	test  al, 2
  	jnz .wait1
  	mov al, 0xD1
 	out 0x64, al
.wait2:
	in  al, 0x64
	and ax, byte 2
	jnz .wait2
	mov al, 0xDF
  	out 0x60, al
	lgdt	[gdtinfo]
	mov	eax,CR0
	or	al,1
	mov	CR0,eax
	jmp	dword (kcode-gdt_table):lancia_kernel
lancia_kernel:
[BITS 32]       ;codice a 32 bit

        mov  ax,kdata-gdt_table	
        mov ds, ax    	
        mov es, ax  
        mov fs, ax     	
        mov   gs, ax 
	mov   ss, ax  	

  mov esp,  0x9FFF0  

        push dword 2  
        popfd 

        mov eax, 0x10000      

        jmp eax     

	cli
	hlt


[BITS 16]
load_kernel:

mov di, DIM_KERNEL
mov ax, 0x1000  
		mov es, ax    

		mov ch, 0   
		mov cl, 2       

		mov dh, 0     
		mov dl, 0       

		xor bx, bx   
	leggi:
                mov ah, 2     
		mov al, 1       
		int 0x13	
	        push bx         
		pop bx      
		mov ax,es      
		add ax,32      
		mov es,ax       
		dec di         
		jz _fine
		inc cx  
		cmp cl,18	
		jbe leggi      
		mov cl,1     
		inc dh 
		cmp dh,2	
		jne leggi       
		mov dh,0   
		inc ch  
		jmp leggi
	_fine:	
	ret


gdtinfo:       

dw  gdtlength   
dd  gdt_table  


	
gdt_table:

null_desc:
    dd  0	
    dd  0	

kcode:
    dw  0xFFFF			;limite
    dw  0x0			;base
    dw  0x9A00
    dw  0x00CF

kdata:
    dw  0xFFFF
    dw  0x0
    dw  0x9200 
    dw  0x00CF

ucode:
	 dw  0xFFFF
	 dw  0x0
	 dw  0xFA00
	 dw  0x00CF

udata:
	 dw  0xFFFF
	 dw  0x0
	 dw  0xF200
	 dw  0x00CF

ktss:	
	 dw   103
	 dw	0
	 dw	0x8900
	 dw	0

 gdtlength equ $ - gdt_table  ;imposto il valore della lunghezza 

off_floppy:
  mov dx,0x3f2
  mov al,0x0c
  out dx,al
  ret

times 510-($-$$) db 0
dw 0xAA55
